//
//  HomemakingViewController.m
//  cityStar
//
//  Created by Hancong on 15/2/11.
//  Copyright (c) 2015年 tiny. All rights reserved.
//

#import "HomemakingViewController.h"
#import "selectDistrict.h"
#import "HomeEconomicsListViewController.h"

@interface HomemakingViewController ()<UIPickerViewDelegate,UIPickerViewDataSource,UITextFieldDelegate,UITextViewDelegate>

@property (nonatomic,strong) NSArray *arrayName;
@property (nonatomic,strong) NSArray *arrayid;
@property (nonatomic,strong) NSString *strstartAddressID;
@property (nonatomic,strong) NSString *homemakingNeedType;
@property (nonatomic,strong) NSString *homemakingTimeType;

@end

@implementation HomemakingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setUpViews];
    [self addBackButton];
    [self addRightButtonWithTitle:@"发布须知" actionBlock:^{
        
    }];
}

-(void)setUpViews
{
    self.title = @"提供服务";
    self.scrollerView.contentSize = CGSizeMake(320, 760);
    self.homemakingNeedType = @"1";
    self.homemakingTimeType = @"1";
    self.arrayName = [[NSArray alloc]init];
    self.arrayid = [[NSArray alloc]init];
    self.textDetail.placeholder = @"输入提供服务的具体内容，如清理房屋，照看孩子等等";
    self.textDetail.placeholderColor = [UIColor colorWithWhite:0.797 alpha:1.000];
    self.arrayName  = [selectDistrict shardDistrictInfo].DistrictNameArray;
    self.arrayid =[selectDistrict shardDistrictInfo].DistrictIdArray;
    self.strstartAddressID = [NSString stringWithFormat:@"%@",self.arrayid[0]];
    
    UIPickerView *typePickerView = [[UIPickerView alloc]init];
    typePickerView.delegate = self;
    typePickerView.tag = 0;
    self.fieldType.inputView = typePickerView;
    
    UIPickerView *addressPickerView = [[UIPickerView alloc]init];
    addressPickerView.delegate = self;
    addressPickerView.tag = 1;
    self.feildRegion.inputView = addressPickerView;
    
    UIPickerView *serviceTimePickerView = [[UIPickerView alloc]init];
    serviceTimePickerView.delegate = self;
    serviceTimePickerView.tag = 2;
    self.fieldServiceRegion.inputView = serviceTimePickerView;
    
    
    UIToolbar * topView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
    [topView setBarStyle:UIBarStyleDefault];
    
    UIBarButtonItem * btnSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    
    UIBarButtonItem * doneButton = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(dismissKeyBoard)];
    
    NSArray * buttonsArray = [NSArray arrayWithObjects:btnSpace,doneButton,nil];
    
    [topView setItems:buttonsArray];
    [self.fieldType setInputAccessoryView:topView];
    [self.feildRegion setInputAccessoryView:topView];
    [self.fieldServiceRegion setInputAccessoryView:topView];
}

-(void)dismissKeyBoard
{
    [self.fieldType resignFirstResponder];
    [self.feildRegion resignFirstResponder];
    [self.fieldServiceRegion resignFirstResponder];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    switch (textField.tag) {
        case 2:
        {
            [UIView animateWithDuration:0.3f animations:^{
//                self.scrollerView.contentOffset = CGPointMake(0, 50);
                textField.text = @"家庭保洁";
            }];
        }
            break;
        case 3:
        {
            [UIView animateWithDuration:0.3f animations:^{
//                self.scrollerView.contentOffset = CGPointMake(0, 100);
                textField.text = self.arrayName[0];
            }];
        }
            break;
        case 4:
        {
            [UIView animateWithDuration:0.3f animations:^{
//                self.scrollerView.contentOffset = CGPointMake(0, 150);
                textField.text = @"市辖区";

            }];
            
        }
            break;
        case 6:
        {
            [UIView animateWithDuration:0.3f animations:^{
                self.scrollerView.contentOffset = CGPointMake(0, 50);
                //                textField.text =  [NSDate date];
            }];
        }
            break;
        case 7:
        {
            [UIView animateWithDuration:0.3f animations:^{
                self.scrollerView.contentOffset = CGPointMake(0, 100);
            }];
        }
        case 8:
        {
            [UIView animateWithDuration:0.3f animations:^{
                self.scrollerView.contentOffset = CGPointMake(0, 150);
            }];
        }
            break;
            
        default:
            break;
    }
}

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}

#pragma mark - pickerViewDelegate
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

-(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (pickerView.tag == 0) {
        return 3;
    }else if(pickerView.tag == 1){
        return self.arrayName.count;
    }else{
        return self.arrayName.count;
    }
}

-(NSString*) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (pickerView.tag == 0) {
        if (row == 0) {
            return @"家庭保洁";
        }else if (row == 1){
            return @"公司保洁";
        }else
        {
            return @"钟点工";
        }
    }else if(pickerView.tag == 1){
        return self.arrayName[row];
    }else{
        return self.arrayName[row];
    }
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (pickerView.tag == 0) {
        if (row == 0) {
            self.fieldType.text = @"家庭保洁";
            self.homemakingNeedType = @"1";
        }else if (row == 1){
            self.fieldType.text = @"公司保洁";
            self.homemakingNeedType = @"2";
        }else
        {
            self.fieldType.text = @"钟点工";
            self.homemakingNeedType = @"3";
        }
    }else if(pickerView.tag == 1){
        NSString *strname = [NSString stringWithFormat:@"%@",self.arrayName[row]];
        self.strstartAddressID = [NSString stringWithFormat:@"%@",self.arrayid[row]];
        self.feildRegion.text = strname;
    }else{
        self.fieldServiceRegion.text = [NSString stringWithFormat:@"%@",self.arrayName[row]];
    }
}

#pragma mark - Actions
-(IBAction)btnPublish_Touch:(id)sender
{
    NSString *errMsg;
    if (self.fieldTitle.text.length == 0) {
        errMsg = @"请输入标题";
    }else if (self.fieldType.text.length == 0){
        errMsg = @"请选择类型";
    }else if (self.feildRegion.text.length == 0){
        errMsg = @"请选择所在区域";
    }else if (self.fieldServiceRegion.text.length == 0){
        errMsg = @"请选择服务区域";
    }else if (self.fieldAddress.text.length == 0){
        errMsg = @"请输入地址";
    }else if (self.fieldContact.text.length == 0){
        errMsg = @"请输入联系人";
    }else if (self.fieldContactNum.text.length == 0){
        errMsg = @"请输入联系电话";
    }else if (self.textDetail.text.length == 0){
        errMsg = @"请输入详情描述";
    }else if (self.fieldChargeMoney.text.length == 0){
        errMsg = @"请输入收费标准";
    }
    if (errMsg) {
        BossSystemShowAlertViewWithTitle(errMsg)
        return;
    }
    NSString *cityID = [[NSUserDefaults standardUserDefaults]objectForKey:@"city_code"];
    [[NSUserDefaults standardUserDefaults]synchronize];
    NSDictionary *parameters = @{@"title":self.fieldTitle.text,
                                 @"is_need":@"0",
                                 @"address":self.fieldAddress.text,
                                 @"service_type":self.homemakingNeedType,
                                 @"service_district_name":self.fieldServiceRegion.text,
                                 @"district":self.strstartAddressID,
                                 @"district_name":self.feildRegion.text,
                                 @"contact":self.fieldContact.text,
                                 @"phone":self.fieldContactNum.text,
                                 @"description":self.textDetail.text,
                                 @"salary":self.fieldChargeMoney.text,
                                 @"city":cityID?cityID:@"122"
                                 };
    [HttpRequestManager postMethod:KInterface_PublishHouse parameters:parameters completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
        DLog(@"%@",response);
        if (success) {
            [SVProgressHUD showSuccessWithStatus:@"发布成功"];
            HomeEconomicsListViewController *homeEconnomicsVC = [[HomeEconomicsListViewController alloc]init];
            [self.navigationController pushViewController:homeEconnomicsVC animated:YES];
        }else{
            [SVProgressHUD showSuccessWithStatus:errMsg];
        }
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
